﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFSystem.JXC.BLL;
using CFSystem.JXC.Model;

namespace CFSystem.JXC.BasisInfo
{
    public partial class FrmBaseSetting : Form
    {
        /// <summary>
        /// 基本设置，规格，品牌，颜色，单位
        /// </summary>
        public enum BaseSetting
        {
            Size,
            Brand,
            Color,
            Unit
        }

        private FrmEditDialog frmEdlg = null;
        BaseSetting _BaseModuleType = BaseSetting.Brand;
        public BaseSetting BaseModuleType
        {
            set
            {
                _BaseModuleType = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_BaseSettingModuleName"></param>
        public FrmBaseSetting(BaseSetting _BaseSettingModuleName)
        {
            InitializeComponent();

            #region init ucBaseSetting
            switch (_BaseSettingModuleName)
            {
                case BaseSetting.Color:
                    {
                        this.Text = "颜色设置";
                        this.LoadAllBrands("编号", "颜色名称", BaseSetting.Color);
                        ucBaseSetting.OnAddEvent += new UCBaseSetting.UCBaseSettingAddhandler(AddBaseSetting_Color);
                        ucBaseSetting.OnDelEvent += new UCBaseSetting.UCBaseSettingDelhandler(DelBaseSetting_Color);
                        ucBaseSetting.OnUpdEvent += new UCBaseSetting.UCBaseSettingUpdhandler(UpdateBaseSetting_Color);
                        break;
                    }
                case BaseSetting.Size:
                    {
                        this.Text = "尺寸设置";
                        this.LoadAllBrands("编号", "尺寸名称", BaseSetting.Size);
                        ucBaseSetting.OnAddEvent += new UCBaseSetting.UCBaseSettingAddhandler(AddBaseSetting_Size);
                        ucBaseSetting.OnDelEvent += new UCBaseSetting.UCBaseSettingDelhandler(DelBaseSetting_Size);
                        ucBaseSetting.OnUpdEvent += new UCBaseSetting.UCBaseSettingUpdhandler(UpdateBaseSetting_Size);
                        break;
                    }
                case BaseSetting.Unit:
                    {
                        this.Text = "单位设置";
                        this.LoadAllBrands("编号", "单位名称", BaseSetting.Unit);
                        ucBaseSetting.OnAddEvent += new UCBaseSetting.UCBaseSettingAddhandler(AddBaseSetting_Unit);
                        ucBaseSetting.OnDelEvent += new UCBaseSetting.UCBaseSettingDelhandler(DelBaseSetting_Unit);
                        ucBaseSetting.OnUpdEvent += new UCBaseSetting.UCBaseSettingUpdhandler(UpdateBaseSetting_Unit);
                        break;
                    }
                default:
                    {
                        this.Text = "品牌设置";
                        this.LoadAllBrands("编号", "品牌名称", BaseSetting.Brand);
                        ucBaseSetting.OnAddEvent += new UCBaseSetting.UCBaseSettingAddhandler(AddBaseSetting_Brand);
                        ucBaseSetting.OnDelEvent += new UCBaseSetting.UCBaseSettingDelhandler(DelBaseSetting_Brand);
                        ucBaseSetting.OnUpdEvent += new UCBaseSetting.UCBaseSettingUpdhandler(UpdateBaseSetting_Brand);
                        break;
                    }
            }
            #endregion

        }

        /// <summary>
        /// 
        /// </summary>
        private void LoadAllBrands(string firstColName, string SecondColName, BaseSetting bs)
        {
            #region construct columns
            //initial grid
            DataTable dt = new DataTable();
            //Create a new column for datatable.
            DataColumn dc = new DataColumn(firstColName, System.Type.GetType("System.String"));
           
            //Add created column to datatable object.
            dt.Columns.Add(dc);
            
            //Create a new column for datatable.
            dc = new DataColumn(SecondColName, System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);
            #endregion

            DataRow dr;

            switch (bs)
            {
                case BaseSetting.Brand:
                    {
                        Brand brand = new Brand(); //BLL calling.
                        IList<BrandInfo> IBrandInfo = brand.GetBrands();
                        foreach (BrandInfo brd in IBrandInfo)
                        {
                            dr = dt.NewRow();
                            dr[firstColName] = brd.BrandID.ToString();
                            dr[SecondColName] = brd.BrandName.ToString();
                            dt.Rows.Add(dr);
                        }
                    }
                    break;
                case BaseSetting.Unit:
                    {
                        Unit unit = new Unit(); //BLL calling.
                        IList<UnitInfo> IUnitInfo = unit.GetUnits();
                        foreach (UnitInfo uit in IUnitInfo)
                        {
                            dr = dt.NewRow();
                            dr[firstColName] = uit.UnitID.ToString();
                            dr[SecondColName] = uit.UnitName.ToString();
                            dt.Rows.Add(dr);
                        }
                    }
                    break;
                case BaseSetting.Color:
                    {
                        Color1 unit = new Color1(); //BLL calling.
                        IList<ColorInfo> IColorInfo = unit.GetColors();
                        foreach (ColorInfo clr in IColorInfo)
                        {
                            dr = dt.NewRow();
                            dr[firstColName] = clr.ColorID.ToString();
                            dr[SecondColName] = clr.ColorName.ToString();
                            dt.Rows.Add(dr);
                        }
                    }
                    break;
                case BaseSetting.Size:
                    {
                        Size1 unit = new Size1(); //BLL calling.
                        IList<SizeInfo> ISizeInfo = unit.GetSizes();
                        foreach (SizeInfo clr in ISizeInfo)
                        {
                            dr = dt.NewRow();
                            dr[firstColName] = clr.SizeID.ToString();
                            dr[SecondColName] = clr.SizeName.ToString();
                            dt.Rows.Add(dr);
                        }
                    }
                    break;
                default:
                    break;
            }
            ucBaseSetting.DataFilled = dt;
            IList<int> colwith= new List<int>();
            colwith.Add(40);
            colwith.Add(184);
            ucBaseSetting.PopulateGridData(colwith);
        }
        /// <summary>
        /// 
        /// </summary>
        void UpdateBaseSetting()
        {
            MessageBox.Show("测试修改");
        }


        /// <summary>
        /// 
        /// </summary>
        void DelBaseSetting()
        {
            MessageBox.Show("测试删除");
        }


        /// <summary>
        /// 
        /// </summary>
        public void AddBaseSetting()
        {
            MessageBox.Show("测试添加");
        }

        /// <summary>
        /// add Band 
        /// </summary>
        public void AddBaseSetting_Brand()
        {
            string brandName = GetInput();
            Brand brd = new Brand();
            brd.Insert(brandName);
            //reload Brands to display
            this.LoadAllBrands("编号", "品牌名称", BaseSetting.Brand);
            this.TopLevel = true;
        }

        /// <summary>
        /// del row by band id
        /// </summary>
        public void DelBaseSetting_Brand()
        {
            int delRowID = ucBaseSetting.CurrentSelectedID;
            Brand brd = new Brand();
            brd.Delete(delRowID);
            this.LoadAllBrands("编号", "品牌名称", BaseSetting.Brand);
            this.TopLevel = true;
        }

        /// <summary>
        /// 
        /// </summary>
        void UpdateBaseSetting_Brand()
        {
            int delRowID = ucBaseSetting.CurrentSelectedID;
            Brand brd = new Brand();
            string updName = GetInput();
            if (updName != string.Empty)
            {
                brd.Update(delRowID, updName);
                this.LoadAllBrands("编号", "品牌名称", BaseSetting.Brand);
                this.TopLevel = true;
            }
        }

        /// <summary>
        /// add unit 
        /// </summary>
        public void AddBaseSetting_Unit()
        {
            string unitName = GetInput();
            Unit uit = new Unit();
            uit.Insert(unitName);
            //reload Brands to display
            this.LoadAllBrands("编号", "单位名称", BaseSetting.Unit);
            this.TopLevel = true;
        }

        /// <summary>
        /// del row by band id
        /// </summary>
        public void DelBaseSetting_Unit()
        {
            int delRowID = ucBaseSetting.CurrentSelectedID;
            Unit uit = new Unit();
            uit.Delete(delRowID);
            this.LoadAllBrands("编号", "单位名称", BaseSetting.Unit);
            this.TopLevel = true;
        }

        /// <summary>
        /// 
        /// </summary>
        public void UpdateBaseSetting_Unit()
        {
            int delRowID = ucBaseSetting.CurrentSelectedID;
            Unit uit = new Unit();
            string updName = GetInput();

            if (updName != string.Empty)
            {
                uit.Update(delRowID, updName);
                this.LoadAllBrands("编号", "单位名称", BaseSetting.Unit);
                this.TopLevel = true;
            }

        }

        /// <summary>
        /// add color 
        /// </summary>
        public void AddBaseSetting_Color()
        {
            string unitName = GetInput();
            Color1  uit = new Color1();
            uit.Insert(unitName);
            //reload Brands to display
            this.LoadAllBrands("编号", "颜色名称", BaseSetting.Color);
            this.TopLevel = true;
        }

        /// <summary>
        /// del row by color id
        /// </summary>
        public void DelBaseSetting_Color()
        {
            int delRowID = ucBaseSetting.CurrentSelectedID;
            Color1 uit = new Color1();
            uit.Delete(delRowID);
            this.LoadAllBrands("编号", "颜色名称", BaseSetting.Color);
            this.TopLevel = true;
        }
        /// <summary>
        /// update color
        /// </summary>
        public void UpdateBaseSetting_Color()
        {
            int delRowID = ucBaseSetting.CurrentSelectedID;
            Color1 uit = new Color1();
            string updName = GetInput();

            if (updName != string.Empty)
            {
                uit.Update(delRowID, updName);
                this.LoadAllBrands("编号", "颜色名称", BaseSetting.Color);
                this.TopLevel = true;
            }

        }


        /// <summary>
        /// add color 
        /// </summary>
        public void AddBaseSetting_Size()
        {
            string unitName = GetInput();
            Size1 uit = new Size1();
            
            uit.Insert(unitName);
            //reload Brands to display
            this.LoadAllBrands("编号", "尺寸名称", BaseSetting.Size);
            this.TopLevel = true;
        }

        /// <summary>
        /// del row by color id
        /// </summary>
        public void DelBaseSetting_Size()
        {
            int delRowID = ucBaseSetting.CurrentSelectedID;
            Size1 uit = new Size1();
            uit.Delete(delRowID);
            this.LoadAllBrands("编号", "尺寸名称", BaseSetting.Size);
            this.TopLevel = true;
        }
        /// <summary>
        /// update color
        /// </summary>
        public void UpdateBaseSetting_Size()
        {
            int delRowID = ucBaseSetting.CurrentSelectedID;
            Size1 uit = new Size1();
            string updName = GetInput();

            if (updName != string.Empty)
            {
                uit.Update(delRowID, updName);
                this.LoadAllBrands("编号", "尺寸名称", BaseSetting.Size);
                this.TopLevel = true;
            }
            else
            {
                MessageBox.Show("尺寸名称不能为空");
            }

        }
        /// <summary>
        /// get the input value for update/add.
        /// </summary>
        /// <returns></returns>
        private string GetInput()
        {
            if (frmEdlg == null)
            {
                frmEdlg = new FrmEditDialog();
            }
            frmEdlg.TopLevel = true;
            frmEdlg.ShowDialog();
            string brandName = frmEdlg.Input;
            frmEdlg.Input = string.Empty;   //clear
            frmEdlg.Hide();
            return brandName;
        }
        /// <summary>
        /// Release the FrmEditDialog instance
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmBaseSetting_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (frmEdlg != null)
            {
                frmEdlg.Close();
            }
        }

    }
}
